home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cobalts.zip / ANIMAKE.C next >
Text File  |  1990-09-03  |  4KB  |  157 lines

  1. /* animake.c
  2.    by Bill Buckels 1990
  3.  
  4.    revised to support virtual screens may 1990.
  5.  
  6.    converts CGA COMPATIBLE .PCX files into a
  7.    C-LANGUAGE CHARACTER ARRAY HEADER OF THE SAME NAME
  8.    but having the extension ".c"
  9.  
  10.    the graphic is embedded and either compiled seperately
  11.    as an object file and declared external & linked to
  12.    or alternately is used as a header.
  13.  
  14.    the graphic is then unpacked directly into a screen buffer,
  15.    or alternately into the frame buffer without disk i/o.
  16.  
  17.    with respect to bitching about compiler use to reorganize
  18.    binary image data into symbolic data that ends up in binary
  19.    format as an object file we should recognize that there are
  20.    many compiler specific formats (ie- .OBJ, .MIX, .O ) and each
  21.    compiler produces suitable OBJECT MODULES from a source file
  22.    and does not need to be supplemented with a FOLKSY HOMESPUN version.
  23.  
  24.  
  25. */
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <io.h>
  29. #include <string.h>
  30.  
  31. unsigned int byteword(unsigned char a, unsigned char b){return b<<8|a;}
  32. unsigned char pcxheader[128];
  33.  
  34. int imagewidth;
  35. int imageheight;
  36.  
  37. int colorpcxsprite(char *name)
  38. {
  39.     FILE *fp; /* reads a ZSOFT .PCX header */
  40.     int i;    /* we only want CGA COLOR sprites */
  41.  
  42.     unsigned int zsoft,codetype,pixbits;
  43.     unsigned int ymin, ymax;
  44.     int invalid = -1, valid = 0, status=valid;
  45.  
  46.     /* read the file header */
  47.     if((fp=fopen(name,"rb"))==NULL)return -1;
  48.     for(i=0;i!=128;i++)pcxheader[i]=fgetc(fp);
  49.     fclose(fp);
  50.  
  51.     zsoft   =pcxheader[0];
  52.     codetype=pcxheader[2];
  53.     pixbits =pcxheader[3];
  54.  
  55.     if(zsoft!=10)        status = invalid;
  56.     if(codetype!=1)      status = invalid;
  57.     if(pixbits != 2 )
  58.        if(pixbits!=1)
  59.           status = invalid;
  60.  
  61.     ymin=byteword(pcxheader[6],pcxheader[7]);
  62.     ymax=byteword(pcxheader[10],pcxheader[11]);
  63.  
  64.     imagewidth= byteword(pcxheader[66],pcxheader[67]);
  65.     imageheight=(ymax-ymin);
  66.     return status;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. char separators[]=" .\n";
  73.  
  74. int PCXARRAY(char *pcxfilename)
  75. {
  76.     unsigned int packet=16,width=0;
  77.     FILE *fp,*fp2;
  78.     long wordcount,target;
  79.     char buffer[128];
  80.     char name[128];
  81.     char name2[128];
  82.     char *wordptr;
  83.  
  84.  
  85.     strcpy(buffer,pcxfilename);
  86.     wordptr=strtok(buffer,separators);
  87.     strcpy(name,buffer);
  88.     strcat(name,".PCX");
  89.     strcpy(name2,buffer);
  90.     strcat(name2,".c");
  91.  
  92.     if(colorpcxsprite(name)==-1)return-1;
  93.     fp = fopen(name,"rb");
  94.     fp2 = fopen(name2,"w");
  95.     printf("\nInput File : %s",name);
  96.     printf("\nOutput File: %s\n",name2);
  97.  
  98.     target = filelength(fileno(fp));
  99.     for(wordcount=0;wordcount<128;wordcount++)fgetc(fp);
  100.  
  101.  
  102.  
  103.     fprintf(fp2,
  104.    "/* Character Array of .PCX format Run Length Encoded CGA Graphic   */\n");
  105.     fprintf(fp2,
  106.    "/* .PCX Input File Name was %s */\n\n",name);
  107.     fprintf(fp2,
  108.    "/* The Image Descriptor Integer Array precedes the Character Array.*/\n");
  109.     fprintf(fp2,
  110.     "/* Descriptor Fields are: 1.    Length of Character Array. */\n");
  111.     fprintf(fp2,
  112.     "/*                        2.    Width of Graphic in BYTES. */\n");
  113.     fprintf(fp2,
  114.     "/*                        3.    Height of Graphic in BYTES.*/\n\n");
  115.  
  116.     fprintf(fp2,"long int %s",buffer);
  117.     fprintf(fp2,"_SIZE[3]={ %ld , %d, %d };\n\n",
  118.         (target-128l),imagewidth,imageheight);
  119.  
  120.     fprintf(fp2,"unsigned char far %s[%ldl]={\n",buffer,(target-128l));
  121.  
  122.     do{
  123.         fprintf(fp2,"%3d",fgetc(fp));
  124.         wordcount++;
  125.         if(wordcount !=target)fprintf(fp2,", ");
  126.         else fprintf(fp2,"};");
  127.         if(width++==packet){fprintf(fp2,"\n");
  128.                             width=0;}
  129.                             }while(wordcount!=target);
  130.         fclose(fp);
  131.         fclose(fp2);
  132.         return(0);
  133. }
  134.  
  135. char *usage[]={
  136. "Animake(C) Copyright by Bill Buckels 1990\n",
  137. "is not a valid CGA color .PCX file.\n",
  138. "Usage is \"ANIMAKE [PCXFILENAME]\"\n",
  139. "Done!\n"};
  140.  
  141.  
  142. main(int argc,char *argv[])
  143. {
  144.     printf("%s",usage[0]);
  145.     switch (argc)
  146.         {
  147.          case 2:  if(PCXARRAY(argv[1])==0)break;
  148.                   printf("%s %s",argv[1],usage[1]);
  149.          default: printf("%s",usage[2]);
  150.                   exit(0);
  151.         }
  152.     printf("%s",usage[3]);
  153.     exit(0);
  154. }
  155.  
  156.  
  157.